#include "tj_form_ybxmcx.h"
#include "ui_tj_form_ybxmcx.h"

#include "pub_var.h"
#include <QSettings>
#include <QMessageBox>
#include "pub_pop_get_item.h"

tj_form_ybxmcx::tj_form_ybxmcx(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::tj_form_ybxmcx)
{
    ui->setupUi(this);

    //恢复当前窗口位置
    QSettings setIni("clin.ini", QSettings::IniFormat);
    QRect rct = setIni.value("windowState/tj_ybxmcx").toRect();
    this->setGeometry(rct);

    //数据库连接
    qrm = new QSqlQuery(pub::db_m);
    qrmt = new QSqlQuery(pub::db_m);
    qro = new QSqlQuery(pub::db_o);
    qrot = new QSqlQuery(pub::db_o);
    md = new QSqlQueryModel();

    listType = "101";   //默认列表类型，西药中成药

    dlgp1 = new Pub_pop_delay;
    dlgp1->setModal(true);
    sqth = new sqThread;
    connect( sqth, &sqThread::message, this, &tj_form_ybxmcx::threadMsg);

    sq = "create temporary table if not exists tmp_new_down ("
         "注册名称 VARCHAR(255) ,批准文号 text, 生产企业名称 VARCHAR(255),药品规格 VARCHAR(1024),"
         "包装 VARCHAR(1024) ,单位 VARCHAR(255),医疗目录编码 VARCHAR(35),商品名 VARCHAR(255),剂型 VARCHAR(200))";

    qrm->exec(sq);


}

tj_form_ybxmcx::~tj_form_ybxmcx()
{
    delete ui;
}

void tj_form_ybxmcx::closeEvent(QCloseEvent *event)
{
    Q_UNUSED(event);
    QSettings setIni("clin.ini", QSettings::IniFormat);
    setIni.setValue("windowState/tj_ybxmcx", QVariant(this->geometry()));

    emit destroyW( "tj_ybxmcx" );

}

void tj_form_ybxmcx::on_pbExit_clicked()
{
    /*

    */
    close();
}

void tj_form_ybxmcx::on_pbSearch_clicked()
{
    ui->lwDescribe->clear();

    if(ui->leKeyName->text().trimmed().isEmpty() &&
            ui->leKeyCert->text().trimmed().isEmpty() &&
            ui->leKeyCode->text().trimmed().isEmpty() &&
            ui->leKeyManu->text().trimmed().isEmpty() )
    {
        QMessageBox::information(this,"注意","检索条件不可以全为空！");
        return;
    }

    QString key_name = ui->leKeyName->text().trimmed();
    key_name.replace(" ","^");

qDebug()<<"key_name"<<key_name;
    QStringList key_name_list;
    if(! key_name.isEmpty())
    {
        key_name_list = key_name.split("^");

    }

qDebug()<<"key_name_list is : "    <<key_name_list;

    ui->pbSearch->setEnabled(false);
    qApp->processEvents();

    QStringList sqls;
    if(ui->cbbType->currentText() == "中草药")
    {
        //医疗目录编码 单味药名称  药材名称 药材种来源

        sqls << "mdelete from tmp_new_down";
        sqlMsg << " 清理旧数据。。。";

        sq = "minsert into tmp_new_down (注册名称 ,批准文号,生产企业名称 ,药品规格,"
             "包装 ,单位 ,医疗目录编码 ,商品名 ,剂型)";
        sq += " select 单味药名称 , '批准文号' ,'生产企业名称' ,'药品规格' ,"
              "'' , '' ,医疗目录编码 ,药材名称 ,'药品剂型' ";
        sq += " from new_down_1302 ";
        sq += " join new_down_1312 s on s.hilist_code = 医疗目录编码 ";  //_
        sq += " and list_type = '102' and begndate <= CURDATE() "
              " and (enddate is null or enddate > CURDATE()) ";
        sq += " where 医疗目录编码 = '"+ui->leKeyCode->text().trimmed().toUpper()+"'";
        sq += " or 单味药名称 like '%" + ui->leKeyName->text() + "%'";
        sq += " limit 9999 ";
        qDebug()<<"检索中草药："<<sq;
        sqls << sq;
        sqlMsg << "导入检索返回的数据。。。";

        sqth->setSqls(sqls);

        sqth->start();
        dlgp1->setNeedClose(false);
        dlgp1->exec();

        sq = "select 注册名称 单味药名称 ,批准文号,生产企业名称 ,药品规格,包装 ,单位 ,"
             " 医疗目录编码 ,商品名 药村名称, 剂型 from tmp_new_down "
             " order by convert(注册名称 using gbk),批准文号,药品规格,包装";
        md->setQuery(sq, pub::db_m);
        ui->tvList->setModel(md);

        int width = ui->tvList->width();
        pub::setColumnWidth(ui->tvList, "0.2^0^0^0^0^0^0.2^0.2^0", width,10 );

    }
    else if(ui->cbbType->currentText() == "中药配方颗粒")
    {
        //中药配方颗粒名称 中药配方颗粒代码  最小计量单位 规格 生产企业名称 上市备案号

        sqls << "mdelete from tmp_new_down";
        sqlMsg << " 清理旧数据。。。";

        sq = "minsert into tmp_new_down (注册名称 ,批准文号,生产企业名称 ,药品规格,"
             "包装 ,单位 ,医疗目录编码 ,商品名 ,剂型)";
        sq += " select 中药配方颗粒名称 , 上市备案号 ,生产企业名称 ,规格 ,"
              "'' , 最小计量单位 ,中药配方颗粒代码 ,药材名称 ,'药品剂型' ";
        sq += " from new_down_1320 ";
        sq += " join new_down_1312 s on s.hilist_code = 中药配方颗粒代码 ";  //_
        sq += " and list_type = '106' and begndate <= CURDATE() "
              " and (enddate is null or enddate > CURDATE()) ";
        sq += " where 中药配方颗粒代码 = '"+ui->leKeyCode->text().trimmed().toUpper()+"'";
        sq += " or 中药配方颗粒名称 like '%" + ui->leKeyName->text() + "%'";
        sq += " limit 9999 ";
        qDebug()<<"检索中药配方颗粒："<<sq;
        sqls << sq;
        sqlMsg << "导入检索返回的数据。。。";

        sqth->setSqls(sqls);

        sqth->start();
        dlgp1->setNeedClose(false);
        dlgp1->exec();

        sq = "select 注册名称 配方颗粒名称 ,批准文号 上市备案号,生产企业名称 ,药品规格,"
             "包装 ,单位, 医疗目录编码 ,商品名 药村名称, 剂型 from tmp_new_down "
             " order by convert(注册名称 using gbk),批准文号,药品规格,包装";
        md->setQuery(sq, pub::db_m);
        ui->tvList->setModel(md);

        int width = ui->tvList->width();
        pub::setColumnWidth(ui->tvList, "0.15^0.15^0.2^0.15^0^0.05^0.1^0.1^0", width,10 );

    }
    else if(ui->cbbType->currentText() == "西药中成药")
    {
        QString t1, t2;
        t1 = "注册名称 is not null ";
        for(int i = 0; i < key_name_list.size(); i++)
        {
            t1 += " and 注册名称 like '%" + key_name_list[i] + "%'";
        }

        t2 = "药品商品名 is not null ";
        for(int i = 0; i < key_name_list.size(); i++)
        {
            t2 += " and 药品商品名 like '%" + key_name_list[i] + "%'";
        }

        sqls << "mdelete from tmp_new_down";
        sqlMsg << " 清理旧数据。。。";

        sq = "minsert into tmp_new_down (注册名称 ,批准文号,生产企业名称 ,药品规格,"
             "包装 ,单位 ,医疗目录编码 ,商品名 ,剂型)";
        sq += " select 注册名称 , 批准文号 ,生产企业名称 ,concat(concat(药品规格,'/'),包装材质) ,"
              " 最小包装数量 , 最小包装单位 ,医疗目录编码 ,药品商品名 ,药品剂型 ";
        sq += " from new_down_1301 ";
        sq += " join new_down_1312 s on s.hilist_code = 医疗目录编码 ";  //_
        sq += " and list_type = '101' and begndate <= CURDATE() "
              " and (enddate is null or enddate > CURDATE()) ";
        sq += " where 有效标志 = '1' and 医疗目录编码 like '"+ui->leKeyCode->text().trimmed().toUpper()+"%'";
        sq += " and ((" + t1 + ") or (" + t2 + "))";
        sq += " and 批准文号 like '%"+ui->leKeyCert->text().trimmed()+"%'";
        sq += " and 生产企业名称 like '%"+ui->leKeyManu->text().trimmed()+"%'";
        sq += " limit 9999 ";
        qDebug()<<"检索西药中成药："<<sq;
        sqls << sq;
        sqlMsg << "导入检索返回的数据。。。";

        sqth->setSqls(sqls);

        sqth->start();
        dlgp1->setNeedClose(false);
        dlgp1->exec();

        sq = "select 注册名称 ,批准文号,生产企业名称 ,药品规格,包装 ,单位 ,医疗目录编码 ,商品名 ,剂型 from tmp_new_down "
             " order by convert(注册名称 using gbk),批准文号,药品规格,包装";
        md->setQuery(sq, pub::db_m);
        ui->tvList->setModel(md);

        int width = ui->tvList->width();
        pub::setColumnWidth(ui->tvList, "0.2^0.15^0.22^0.1^0.05^0.05^0.1", width,10 );

    }
    else if(ui->cbbType->currentText() == "院内制剂")
    {
        sqls << "mdelete from tmp_new_down";
        sqlMsg << " 清理旧数据。。。";

        QString t1;
        t1 = "药品商品名 is not null ";
        for(int i = 0; i < key_name_list.size(); i++)
        {
            t1 += " and 药品商品名 like '%" + key_name_list[i] + "%'";
        }

        sq = "minsert into tmp_new_down "
             "(注册名称 ,批准文号,生产企业名称 ,药品规格,包装 ,单位 ,医疗目录编码 ,商品名 ,剂型)";
        sq += "select 药品商品名 ,批准文号 ,生产企业名称 ,药品规格 ,最小包装数量 ,最小包装单位 ,医疗目录编码 ,'-' ,剂型 ";
        sq += " from new_down_1303 ";
        sq += " join new_down_1312 s on s.hilist_code = 医疗目录编码 ";    //_
        sq += " and list_type = '103' and begndate <= CURDATE() "
              " and (enddate is null or enddate > CURDATE()) ";
        sq += " where 有效标志 = '1' and 医疗目录编码 like '"+ui->leKeyCode->text().trimmed().toUpper()+"%'";
        sq += " and " + t1 ;//药品商品名 like '%"+ui->leKeyName->text().trimmed()+"%'
        sq += " and 批准文号 like '%"+ui->leKeyCert->text().trimmed()+"%'";
        sq += " and 生产企业名称 like '%"+ui->leKeyManu->text().trimmed()+"%'";
        sq += " limit 9999 ";
        qDebug()<<"检索院内制剂："<<sq;
        sqls << sq;
        sqlMsg << "导入检索返回的数据。。。";

        sqth->setSqls(sqls);

        sqth->start();
        dlgp1->setNeedClose(false);
        dlgp1->exec();
        sq = "select 注册名称 ,批准文号,生产企业名称 ,药品规格,包装 ,单位 ,医疗目录编码 ,商品名 ,剂型 from tmp_new_down "
             " order by convert(注册名称 using gbk),批准文号,药品规格,包装";

        md->setQuery(sq, pub::db_m);
        ui->tvList->setModel(md);
        int width = ui->tvList->width();
        pub::setColumnWidth(ui->tvList, "0.25^0.2^0.2^0.15^0.05^0.05", width,10 );

    }
    else if(ui->cbbType->currentText() == "医用耗材")
    {
        sqls << "mdelete from tmp_new_down";
        sqlMsg << " 清理旧数据。。。";
        QString t1;
        t1 = "耗材名称 is not null ";
        for(int i = 0; i < key_name_list.size(); i++)
        {
            t1 += " and 耗材名称 like '%" + key_name_list[i] + "%'";
        }

        sq = "minsert into tmp_new_down "
             "(注册名称 ,批准文号,生产企业名称 ,药品规格,包装 ,单位 ,医疗目录编码 ,商品名 ,剂型)";
        sq += " select 耗材名称 ,注册备案号 ,生产企业名称 ,concat(concat(规格,'/'),产品型号) ,'-',耗材分类 ,医疗目录编码 ,'-' ,'-' ";
        sq += " from new_down_1306_";  // _
        sq += " join new_down_1312 s on s.hilist_code = 医疗目录编码 ";  //  //_
        sq += " and list_type = '301' and begndate <= CURDATE() "
              " and (enddate is null or enddate > CURDATE()) ";
        sq += " where 医疗目录编码 like '"+ui->leKeyCode->text().trimmed().toUpper()+"%'";
        sq += " and " + t1; //耗材名称 like '%"+ui->leKeyName->text().trimmed()+"%'
        sq += " and 注册备案号 like '%"+ui->leKeyCert->text().trimmed()+"%'";
        sq += " and 生产企业名称 like '%"+ui->leKeyManu->text().trimmed()+"%'";
        sq += " limit 9999 ";
        qDebug()<<"检索耗材："<<sq;
        sqls << sq;
        sqlMsg << "导入检索返回的数据。。。";

        sqth->setSqls(sqls);

        sqth->start();
        dlgp1->setNeedClose(false);
        dlgp1->exec();
        sq = "select 注册名称 耗材名称,批准文号 注册备案号,生产企业名称 ,药品规格 规格,'-','-' ,"
             "医疗目录编码 ,'-', '-' from tmp_new_down "
             " order by convert(注册名称 using gbk),批准文号,药品规格,包装";

        md->setQuery(sq, pub::db_m);
        ui->tvList->setModel(md);
        int width = ui->tvList->width();
        pub::setColumnWidth(ui->tvList, "0.25^0.25^0.25^0.15", width,10 );

    }
    else if(ui->cbbType->currentText() == "诊疗项目")
    {
        sqls << "mdelete from tmp_new_down";
        sqlMsg << " 清理旧数据。。。";

        QString t1;
        t1 = "医疗服务项目名称 is not null ";
        for(int i = 0; i < key_name_list.size(); i++)
        {
            t1 += " and 医疗服务项目名称 like '%" + key_name_list[i] + "%'";
        }

        sq = "minsert into tmp_new_down "
             "(注册名称 ,批准文号,生产企业名称 ,药品规格,包装 ,单位 ,医疗目录编码 ,商品名 ,剂型)";
        sq += "select 医疗服务项目名称 ,诊疗项目说明 ,计价单位 ,服务项目类别 ,'1' ,'2' ,医疗目录编码 ,'3' ,'4' "
              " from  "
              "     (select 医疗服务项目名称 ,诊疗项目说明 ,计价单位 ,服务项目类别 ,'1' ,'2' ,医疗目录编码 ,'3' ,'4' "
              "     from new_down_1305 where 有效标志 = '1'  "
              "     union "
              "     select 医疗服务项目名称 ,诊疗项目说明 ,计价单位 ,'0' ,'1' ,'2' ,医疗目录编码 ,'3' ,'4' "
              "     from new_down_1321 where 有效标志 = '1' ) base";
        sq += " join new_down_1312 s on s.hilist_code = 医疗目录编码 "    //
              " and list_type = '201' and begndate <= CURDATE() "
              " and (enddate is null or enddate > CURDATE()) ";
        sq += " where 医疗目录编码 like '"+ui->leKeyCode->text().trimmed().toUpper()+"%'";
        sq += " and " + t1 ;//医疗服务项目名称 like '%"+ui->leKeyName->text().trimmed()+"%'";
        sq += " limit 9999 ";

        qDebug()<<"检索医疗服务项目："<<sq;

        sqls << sq;
        sqlMsg << "导入检索返回的数据。。。";

        sqlMsg << "发生错误，正在回滚数据。。。";
        sqth->setSqls(sqls);

        sqth->start();
        dlgp1->setNeedClose(false);
        dlgp1->exec();
        sq = "select 注册名称 医疗服务项目名称 ,批准文号 诊疗项目说明,生产企业名称 计价单位,药品规格 服务项目类别,"
             "包装 ,单位 ,医疗目录编码 ,商品名 ,剂型 from tmp_new_down "
             " order by convert(药品规格 using gbk),注册名称";

        md->setQuery(sq, pub::db_m);
        ui->tvList->setModel(md);
        int width = ui->tvList->width();
        pub::setColumnWidth(ui->tvList, "0.25^0.25^0.15^0.15", width,10 );

    }

    ui->pbSearch->setEnabled(true);

}

void tj_form_ybxmcx::on_tvList_clicked(const QModelIndex &index)
{
    QString code = index.siblingAtColumn(6).data().toString();
    if(code.isEmpty())
    {
        return;
    }

    if(ui->cbbType->currentText() == "医用耗材")
    {
        sq = "select 耗材名称 ,注册备案号 ,生产企业名称 ,规格 ,产品型号,耗材分类 ,耗材材质 "
             " from new_down_1306 "
             " where 医疗目录编码 = '"+index.siblingAtColumn(6).data().toString()+"' and 有效标志 = '1'";
        qrmt->exec(sq);
        if(!qrmt->next())
        {
            ui->lwDescribe->addItem( "无有效医保基础信息。");
            return;
        }
    }

    ui->lwDescribe->clear();
    ui->lwDescribe->addItem("医保项目对照信息："  );
    ui->lwDescribe->addItem("医保编码：" + index.siblingAtColumn(6).data().toString());
    ui->lwDescribe->addItem("项目名称：" + index.siblingAtColumn(0).data().toString());
    if(!index.siblingAtColumn(1).data().toString().isEmpty()
            && index.siblingAtColumn(1).data().toString() != "null"
            && index.siblingAtColumn(1).data().toString() != "-")
    {
        QString t = "批准文号：";
        if(ui->cbbType->currentText() == "诊疗项目")
        {
            t = "诊疗项目说明：";
        }
        ui->lwDescribe->addItem( t + index.siblingAtColumn(1).data().toString());
    }
    if(!index.siblingAtColumn(2).data().toString().isEmpty()
            && index.siblingAtColumn(2).data().toString() != "null"
            && index.siblingAtColumn(2).data().toString() != "-")
    {
        QString t = "生产厂家：";
        if(ui->cbbType->currentText() == "诊疗项目")
        {
            t = "计价单位：";
        }
        ui->lwDescribe->addItem(t + index.siblingAtColumn(2).data().toString());
    }
    if(!index.siblingAtColumn(8).data().toString().isEmpty()
            && index.siblingAtColumn(8).data().toString() != "null"
            && index.siblingAtColumn(8).data().toString() != "-")
    {
        ui->lwDescribe->addItem("剂型：" + index.siblingAtColumn(8).data().toString());
    }
    if(!index.siblingAtColumn(3).data().toString().isEmpty()
            && index.siblingAtColumn(3).data().toString() != "null"
            && index.siblingAtColumn(3).data().toString() != "-")
    {
        QString t = "规格：";
        if(ui->cbbType->currentText() == "诊疗项目")
        {
            t = "服务项目类别：";
        }
        ui->lwDescribe->addItem(t + index.siblingAtColumn(3).data().toString());
    }
    if(!index.siblingAtColumn(4).data().toString().isEmpty()
            && index.siblingAtColumn(4).data().toString() != "null"
            && index.siblingAtColumn(4).data().toString() != "-")
    {
        QString t = "包装："+ index.siblingAtColumn(4).data().toString();
        if(ui->cbbType->currentText() == "医用耗材")
        {
            t = "产品型号：" + qrmt->value(4).toString();
        }
        ui->lwDescribe->addItem(t );
    }

    if(!index.siblingAtColumn(5).data().toString().isEmpty()
            && index.siblingAtColumn(5).data().toString() != "null"
            && index.siblingAtColumn(5).data().toString() != "-")
    {

        QString t = "单位：" + index.siblingAtColumn(5).data().toString();
        if(ui->cbbType->currentText() == "医用耗材")
        {
            t = "耗材分类：" + qrmt->value(5).toString();
        }
        ui->lwDescribe->addItem(t );
    }

    if(ui->cbbType->currentText() == "医用耗材")
    {
        ui->lwDescribe->addItem("耗材材质：" + qrmt->value(6).toString());
        //ui->lwDescribe->addItem("计价单位：" + qrmt->value(7).toString());
    }

    sq = "select chrgitm_lv, begndate, enddate,memo from new_down_1312"
         "  where list_type = '" + listType + "' and hilist_code = '" + code + "'";
    if(ui->rbInuse->isChecked())
    {
        sq += " and (enddate is null or enddate > curdate()) ";
    }
    sq += " order by begndate";
    qDebug()<<"项目医保对照信息 : "<<sq;

    qrm->exec(sq);

    bool valid = false;
    while(qrm->next())
    {
        QString lv = qrm->value(0).toString();
        QString st = qrm->value(1).toDate().toString("yyyy-MM-dd");
        QString ed = qrm->value(2).toDate().toString("yyyy-MM-dd");
        QString mm = qrm->value(3).toString();
        ui->lwDescribe->addItem("启用时间：" + st );
        if(!ed.isEmpty())
        {
            ui->lwDescribe->addItem("停止时间：" + ed );
        }
        if(QDate::fromString(st, "yyyy-MM-dd") < QDate::currentDate() //启用时间小于当前时间
                && (ed.isEmpty() || QDate::fromString(ed, "yyyy-MM-dd") > QDate::currentDate()))   //停用时间空白 或大于当前时间
        {
            valid = true;
        }

        if( lv == "01"){
            ui->lwDescribe->addItem("医保项目等级：甲类"  );
        }else if(lv == "02"){
            ui->lwDescribe->addItem("医保项目等级：乙类"  );
        } else if(lv == "03"){
            ui->lwDescribe->addItem("医保项目等级：丙类"  );
        }else{
            ui->lwDescribe->addItem("医保项目等级代码：" + lv );
        }

        if(!mm.isEmpty())
        {
            ui->lwDescribe->addItem("使用限制：" + mm );
        }


    }
    if(!valid)
    {
        ui->lwDescribe->addItem("找不到有效医保对照信息 ，项目无法使用。");
        return;
    }

    ui->lwDescribe->addItem("\n限价信息："  );
    //限价信息
    sq = "select (select dic_label from com_dict_new where dic_type = 'HILIST_LMTPRIC_TYPE' and dic_value = a.hilist_lmtpric_type),"
         " (select dic_label from com_dict_new where dic_type = 'OVERLMT_DSPO_WAY' and dic_value = a.overlmt_dspo_way),"
         " hilist_pric_uplmt_amt,begndate,enddate"
         " from (SELECT hilist_lmtpric_type, overlmt_dspo_way,hilist_pric_uplmt_amt,begndate,enddate "
         " FROM clinic.new_down_1318 where hilist_code = '" + code + "' ";
    if(ui->rbInuse->isChecked())
    {
        sq += " and  begndate <= curdate() "
              " and (enddate is null or enddate > curdate())) "
              " a order by begndate ";
    }else{
        sq += " ) a order by begndate ";
    }
    qDebug()<<"查询限价信息 :"<<sq;
    qrm->exec(sq);

    bool price_control = false;
    while(qrm->next())
    {
        QString st = qrm->value(3).toDate().toString("yyyy-MM-dd");
        QString ed = qrm->value(4).toDate().toString("yyyy-MM-dd");
        ui->lwDescribe->addItem("启用时间：" + st );
        if(!ed.isEmpty())
        {
            ui->lwDescribe->addItem("停止时间：" + ed );
        }
        ui->lwDescribe->addItem("限价类型：" + qrm->value(0).toString());
        ui->lwDescribe->addItem("超限价处理方式：" + qrm->value(1).toString());
        ui->lwDescribe->addItem("限价：" + qrm->value(2).toString());
        if(QDate::fromString(st, "yyyy-MM-dd") < QDate::currentDate() //启用时间小于当前时间
                && (ed.isEmpty() || QDate::fromString(ed, "yyyy-MM-dd") > QDate::currentDate()))   //停用时间空白 或大于当前时间
        {
            price_control = true;
        }
    }

    if(!price_control)
    {
        ui->lwDescribe->addItem("指定的医保项目没有限价信息。"  );
    }

    //2025-8-1,先行自付比例查询，数据量巨大，参考意义不大
    /*
    ui->lwDescribe->addItem("\n先行自付比例："  );
    //先行自付比例

    sq = "select (select dic_label from com_dict_new where dic_type = 'SELFPAY_PROP_PSN_TYPE' and dic_value = a.selfpay_prop_psn_type),"
         " (select dic_label from com_dict_new where dic_type = 'SELFPAY_PROP_TYPE' and dic_value = a.selfpay_prop_type),"
         " selfpay_prop,begndate,enddate"
         " from "
         "(select selfpay_prop_psn_type, selfpay_prop_type,selfpay_prop,begndate,enddate "
         " from clinic.new_down_1319 where hilist_code = '" + code + "' ";
    if(ui->rbInuse->isChecked())
    {
        sq += " and (enddate is null or enddate > curdate())) a order by begndate ";
    }else{
        sq += " ) a order by begndate ";
    }
    qDebug()<<"查找先行自付比例 : "<<sq;
    qrm->exec(sq);

    bool prepay = false;
    while(qrm->next())
    {
        QString st = qrm->value(3).toDate().toString("yyyy-MM-dd");
        QString ed = qrm->value(4).toDate().toString("yyyy-MM-dd");
        ui->lwDescribe->addItem("启用时间：" + st );
        if(!ed.isEmpty())
        {
            ui->lwDescribe->addItem("停止时间：" + ed );
        }
        ui->lwDescribe->addItem("人员类别：" + qrm->value(0).toString());
        ui->lwDescribe->addItem("比例类别：" + qrm->value(1).toString());
        ui->lwDescribe->addItem("自付比例：" + qrm->value(2).toString());
        if(QDate::fromString(st, "yyyy-MM-dd") < QDate::currentDate() //启用时间小于当前时间
                && (ed.isEmpty() || QDate::fromString(ed, "yyyy-MM-dd") > QDate::currentDate()))   //停用时间空白 或大于当前时间
        {
            prepay = true;
        }
    }

    if(!prepay)
    {
        ui->lwDescribe->addItem("未找到先行自付信息。"  );
    }
    */
}

void tj_form_ybxmcx::on_tvList_doubleClicked(const QModelIndex &index)
{
    on_tvList_clicked(index);
}

void tj_form_ybxmcx::on_cbbType_currentTextChanged(const QString &arg1)
{
    ui->leKeyCert->clear();
    ui->leKeyCode->clear();
    ui->leKeyName->clear();
    ui->leKeyManu->clear();
    ui->lwDescribe->clear();
    ui->tvList->setModel(NULL);
    if(arg1 == "西药中成药")    {
        listType = "101";
    }
    else if(arg1 == "院内制剂")
    {
        listType = "103";
    }
    else  if(arg1 == "医用耗材")
    {
        listType = "301";
    }
    else if(arg1 == "诊疗项目")
    {
        listType = "201";
    }
    else if(arg1 == "中草药")
    {
        listType = "102";
    }
    else if(arg1 == "中药配方颗粒")
    {
        listType = "106";
    }
}
void tj_form_ybxmcx::threadMsg(QString s)
{
    if(s == "finish"){
        dlgp1->setNeedClose(true);
        dlgp1->closeWindow();
    }
    else if(s.left(3) == "msg")
    {
        dlgp1->setText(sqlMsg[pub::getStr(s, "^", 2).toInt()]);
    }
    else if(s.left(8) == "rollback")
    {
        dlgp1->setText("线程 SQL 发生错误，正在回滚。。。");
    }
    else if(s.left(3) == "err"){
        dlgp1->setNeedClose(true);
        dlgp1->closeWindow();
        qDebug()<<" SQL thread " +s;
        QMessageBox::warning(this, "注意", "线程中的sql执行发生错误！\n" + s);
    }
    else
    {
        qDebug()<<" SQL thread return invalid message : " +s;
    }
}
